﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using cebao.Model;
public class BaseHttpHandler : IHttpHandler, IRequiresSessionState
{
    public HttpRequest Request;
    public HttpResponse Response;
    public HttpSessionState Session;
    public HttpServerUtility Server;
    public HttpCookie Cookie;
    public userinfo user;
    public string virtualPath = Maticsoft.Common.ConfigHelper.GetConfigString("VirtualPath");
    public cebao.BLL.SysLog bllLog = new cebao.BLL.SysLog();
    public virtual void ProcessRequest(HttpContext context)
    {
        Request = context.Request;
        Response = context.Response;
        Session = context.Session;
        Server = context.Server;

        if (Session["UserInfo"] != null)
        {
            user = Session["UserInfo"] as cebao.Model.userinfo;
        }
        else
        {
            if (Request.Cookies["UserInfo"] != null)
            {
                user = new userinfo();
                HttpCookie userCookie = Request.Cookies["UserInfo"];
                user.uid = Convert.ToInt32(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["UserID"]));
                //user.AddTime = Convert.ToDateTime(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["AddTime"]));
                //user.AddUserID = Convert.ToInt32(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["AddUserID"]));
                //user.DeptID = Convert.ToInt32(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["DeptID"]));
                user.utruename = Convert.ToString(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["DisplayName"]));
                user.uname = Convert.ToString(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["LoginName"]));
                user.upwd = Convert.ToString(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["Password"]));
                //user.RegisterUserID = Convert.ToInt32(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["RegisterUserID"]));
                user.remark = Convert.ToString(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["Remark"]));
                //user.ulevel = Convert.ToString(cebao.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["State"]));
                Session["UserInfo"] = user;
            }
            else
            {
                WriteError("系统未登录或者登陆超时，不允许访问！");
            }
        }
    }
    /// <summary>
    /// 输出：{isOk:1,message:"操作成功"}
    /// </summary>
    public void WriteSuccess()
    {
        WriteMessage(1, "操作成功！");
    }
    /// <summary>
    /// 输出：{isOk:1,message:"操作成功"}
    /// </summary>
    /// <param name="ex">错误</param>
    public void WriteError(string message)
    {
        WriteMessage(0, message);
    }
    /// <summary>
    /// 输出：{isOk:1,message:"操作成功"}
    /// </summary>
    /// <param name="Status">0失败，1成功</param>
    /// <param name="Message">消息内容</param>
    public void WriteMessage(int Status, string Message)
    {
        Response.Write(cebao.Common.JsonHelper.ToResultJson(Status, Message));
    }
    /// <summary>
    /// 输入日志
    /// </summary>
    /// <param name="Message"></param>
    public void WriteLog(string Message)
    {
        SysLog modelLog = new SysLog();
        modelLog.AddTime = DateTime.Now;
        modelLog.AddUserID = user.uid;
        modelLog.IP = Request.UserHostAddress;
        modelLog.Message = Message;
        modelLog.Page = Request.UrlReferrer == null ? "" : Request.UrlReferrer.PathAndQuery;
        bllLog.Add(modelLog);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
